package com.nic.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.model.InputMouldBean;
import com.nic.view.model.MouldBean;
import com.nic.view.model.RadioMouldBean;
import com.nic.view.widget.ContainsEmojiEditText;
import com.nic.view.widget.TakePhotoView;

import java.util.List;


/**
 * Created by Administrator on 2018/11/12.
 */

public class ProjectMouldRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int INPUT_MOULD = 0;
    public static final int RADIO_MOULD = 1;
    public static final int TAKE_PHOTO_MOULD = 2;
    private List<MouldBean> mouldBeanList;

    public void setMouldBeanList(List<MouldBean> mouldBeanList) {
        this.mouldBeanList = mouldBeanList;
        notifyDataSetChanged();
    }

    public List<MouldBean> getMouldBeanList() {
        return mouldBeanList;
    }

    private Context context;

    public ProjectMouldRecycleAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        if (viewType == INPUT_MOULD) {
            View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_a_input, null);
            rootView.setLayoutParams(params);
            ViewInputHolder viewInputHolder = new ViewInputHolder(rootView);
            return viewInputHolder;
        } else if (viewType == RADIO_MOULD) {
            View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_a_radio, null);
            rootView.setLayoutParams(params);
            ViewRadioHolder viewRadioHolder = new ViewRadioHolder(rootView);
            return viewRadioHolder;
        } else if (viewType == TAKE_PHOTO_MOULD) {
            TakePhotoView takePhotoView = new TakePhotoView(parent.getContext());
            takePhotoView.setRequestCode(1000);
            takePhotoView.setBackgroundColor(Color.WHITE);
            takePhotoView.setLayoutParams(params);
            ViewPhotoHolder viewPhotoHolder = new ViewPhotoHolder(takePhotoView);
            return viewPhotoHolder;
        }
        return null;

    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (mouldBeanList == null && mouldBeanList.isEmpty()) {
            return;
        }
        MouldBean mouldBean = mouldBeanList.get(position);
        if (type == INPUT_MOULD) {
            ViewInputHolder viewInputHolder = (ViewInputHolder) holder;
            itemInput(position, mouldBean, viewInputHolder);
        } else if (type == RADIO_MOULD) {
            ViewRadioHolder viewRadioHolder = (ViewRadioHolder) holder;
            if (mouldBean.getRadioMouldBean() != null) {
                RadioMouldBean radioMouldBean = mouldBean.getRadioMouldBean();
                viewRadioHolder.tvSubTitle.setText("" + radioMouldBean.getSubTitle());
                if (radioMouldBean.getList() != null && !radioMouldBean.getList().isEmpty()) {
                    viewRadioHolder.rgParent.removeAllViewsInLayout();
                    for (RadioMouldBean.RadioMouldItemBean bean : radioMouldBean.getList()) {
                        RadioButton radioButton = (RadioButton) LayoutInflater.from(context).inflate(R.layout.radiobutton, null);
                        radioButton.setText("" + bean.getSubTitle());
                        viewRadioHolder.rgParent.addView(radioButton);
                    }
                    ((RadioButton) viewRadioHolder.rgParent.getChildAt(0)).setChecked(true);
                }
            }

        } else if (type == TAKE_PHOTO_MOULD) {
            ViewPhotoHolder viewPhotoHolder = (ViewPhotoHolder) holder;
//            viewPhotoHolder.takePhotoView.onActivityResult();
        }
    }

    private void itemInput(int position, MouldBean mouldBean, ViewInputHolder viewInputHolder) {
        if (mouldBean.getInputMouldBean() == null) {
            return;
        }
        InputMouldBean bean = mouldBean.getInputMouldBean();
        viewInputHolder.tvSubtitle.setText("" + bean.getSubTitle());
        if (bean.isArrow()) {
            viewInputHolder.ivRightArrow.setVisibility(View.VISIBLE);
        } else {
            viewInputHolder.ivRightArrow.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(bean.getUnit())) {
            viewInputHolder.ivRightUnit.setVisibility(View.VISIBLE);
            viewInputHolder.ivRightUnit.setText("" + bean.getUnit());
        } else {
            viewInputHolder.ivRightUnit.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(bean.getContent())) {
            viewInputHolder.tvContent.setError(bean.getErrorHint());
            viewInputHolder.tvContent.setText("");
        } else {
            viewInputHolder.tvContent.setError(null);
            viewInputHolder.tvContent.setText(bean.getContent());
        }

        if (!TextUtils.isEmpty(bean.getHint())) {
            viewInputHolder.tvContent.setHint(bean.getHint());
        } else {
            viewInputHolder.tvContent.setHint("");
        }
        if (bean.isFocusable()) {
            viewInputHolder.tvContent.setFocusableInTouchMode(true);
        } else {
            if (bean.isEditor()) {
                viewInputHolder.tvContent.clearFocus();
            } else {
                viewInputHolder.tvContent.setFocusable(false);
            }
        }
        viewInputHolder.tvContent.setOnFocusChangeListener(new MyOnFocusListener(viewInputHolder.tvContent, new MyTextWatcher(bean), bean));
        viewInputHolder.tvContent.setInputType(bean.getInputType());
        if (bean.getMaxLength() != 0) {
            viewInputHolder.tvContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(bean.getMaxLength())});
        }
        if (!TextUtils.isEmpty(bean.getDigits())) {
            viewInputHolder.tvContent.setKeyListener(DigitsKeyListener.getInstance(bean.getDigits()));
        }
    }

    public boolean checkInputNull() {
        boolean isNull = false;
        if (mouldBeanList == null || mouldBeanList.isEmpty()) {
            isNull = true;
            return isNull;
        }

        for (MouldBean mouldBean : mouldBeanList) {
            if (mouldBean.getInputMouldBean() != null) {
                InputMouldBean inputBean = mouldBean.getInputMouldBean();
                if (TextUtils.isEmpty(inputBean.getContent()) && inputBean.isEditor()) {
                    inputBean.setErrorHint(inputBean.getSubTitle() + "不能为空");
                    isNull = true;
                } else {
                    inputBean.setErrorHint(null);
                }
            }
        }
        notifyDataSetChanged();
        return isNull;
    }

    @Override
    public int getItemViewType(int position) {
        if (mouldBeanList == null || mouldBeanList.isEmpty()) {
            return super.getItemViewType(position);
        }
        MouldBean item = mouldBeanList.get(position);
        return item.getType();
    }

    @Override
    public int getItemCount() {
        return mouldBeanList == null ? 0 : mouldBeanList.size();
    }


    private class MyOnFocusListener implements View.OnFocusChangeListener {
        EditText textView;
        TextWatcher textWatcher;
        InputMouldBean bean;

        public MyOnFocusListener(EditText textView, TextWatcher textWatcher, InputMouldBean bean) {
            this.textView = textView;
            this.textWatcher = textWatcher;
            this.bean = bean;
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                textView.addTextChangedListener(textWatcher);
                bean.setFocusable(true);
            } else {
                textView.removeTextChangedListener(textWatcher);
                bean.setFocusable(false);
            }
        }
    }

    private class MyTextWatcher implements TextWatcher {
        InputMouldBean bean;

        public MyTextWatcher(InputMouldBean bean) {
            this.bean = bean;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(s.toString())) {
                bean.setContent(s.toString());
            } else {
                bean.setContent(null);
            }
        }
    }

    static class ViewInputHolder extends RecyclerView.ViewHolder {
        TextView tvSubtitle;
        ContainsEmojiEditText tvContent;
        ImageView ivRightArrow;
        TextView ivRightUnit;

        ViewInputHolder(View view) {
            super(view);
            tvSubtitle = (TextView) view.findViewById(R.id.tv_subtitle);
            tvContent = (ContainsEmojiEditText) view.findViewById(R.id.tv_content);
            ivRightArrow = (ImageView) view.findViewById(R.id.iv_right_arrow);
            ivRightUnit = (TextView) view.findViewById(R.id.iv_right_unit);
        }
    }

    static class ViewRadioHolder extends RecyclerView.ViewHolder {
        RadioGroup rgParent;
        TextView tvSubTitle;

        ViewRadioHolder(View view) {
            super(view);
            rgParent = (RadioGroup) view.findViewById(R.id.rg_parent);
            tvSubTitle = (TextView) view.findViewById(R.id.tv_subTitle);
        }
    }

    static class ViewPhotoHolder extends RecyclerView.ViewHolder {
        TakePhotoView takePhotoView;

        ViewPhotoHolder(View view) {
            super(view);
            takePhotoView = (TakePhotoView) view;
        }
    }

}
